package com.paypalpay.controller.interfaces;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.paypalpay.entity.common.DeviceRealTimeInformation;
import com.paypalpay.entity.common.OrderPaymentManager;
import com.paypalpay.entity.common.UnifiedOrderPackages;
import com.paypalpay.entity.common.UserboundPaymentOrder;
import com.paypalpay.external.entity.order.request.RequestOrder;
import com.paypalpay.external.entity.order.request.RequestOrderData;
import com.paypalpay.external.entity.order.request.RequestOrderOrderList;
import com.paypalpay.external.entity.order.response.ResponseOrder;
import com.paypalpay.external.entity.order.response.ResponseOrderData;
import com.paypalpay.external.entity.order.response.ResponseOrderTradeData;
import com.paypalpay.external.utils.UtilsInterfacesPackage;
import com.paypalpay.pojo.Constant;
import com.paypalpay.service.*;
import com.paypalpay.turntointerfaceservice.TurnToOrderService;
import com.paypalpay.turntointerfaceservice.TurnToQueryDeviceRealTimeService;
import com.paypalpay.turntointerfaceservice.newservice.OrderServiceNew;
import com.paypalpay.turntointerfaceservice.newservice.QueryDeviceRealTimeServiceNew;
import com.paypalpay.util.UexUtil;
import com.paypalpay.util.wx.MD5;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Demo 订购下单
 *
 * @author lm
 * @date 2018/08/31
 */
@RestController
@Api("前端有卡模式下订单流程")
public class OrderController {
    @Autowired
    private HttpSession session;
    @Autowired
    private SequenceService sequenceService;
    @Autowired
    private UnifiedOrderPackagesService unifiedOrderPackagesService;

    @Autowired
    private InternationalPayPackageService internationalPayPackageService;

    @Autowired
    UserboundPaymentOrderService userboundPaymentOrderService;
    @Autowired
    PartnerService partnerService;

    @CrossOrigin
    @RequestMapping(value = "/orderPackages", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "下单订购流量", response = ResponseOrder.class)
    public String order(@ApiParam @RequestBody RequestOrder requestOrder) throws Exception {
        String requestStr = JSON.toJSONString(requestOrder);
        System.out.println("前端请求报文是：" + requestStr);
        // 1，生成流水号
        String ConnSeqNo = sequenceService.getPackageTaskNo();
        // 2,生成当前时间戳
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String curtime = df.format(new Date());
        UtilsInterfacesPackage utils = new UtilsInterfacesPackage();

        String jsonString = "";

        requestOrder = JSONArray.parseObject(requestStr, RequestOrder.class);

        String requestSign = requestOrder.getSign();
        if (requestOrder.getSign() == null) {
            System.out.println("请求报文不合法:sign为空");
            jsonString = utils.dataWeiKong(ConnSeqNo, curtime);
            return jsonString;
        } else {

            System.out.println("请求的签名是：" + requestSign);
            RequestOrderData requestData = requestOrder.getData();
            if (requestOrder.getData() == null) {
                System.out.println("请求报文不合法:data为空");
                jsonString = utils.dataWeiKong(ConnSeqNo, curtime);
                return jsonString;
            } else {
                String partnerCode = requestData.getPartnerCode();
                String secret = partnerService.findSecretByPartnerCode(partnerCode);
                String connSeqNo = requestData.getConnSeqNo();
                String token = requestData.getToken();
                String tradeTime = requestData.getTradeTime();
                String tradeType = requestData.getTradeType();
                String lang = requestData.getLang();

                if (requestData.getTradeData() == null) {

                    System.out.println("请求报文不合法:TradeData为空");
                    jsonString = utils.dataWeiKong(ConnSeqNo, curtime);
                    return jsonString;

                } else {
                    //将报文中有用的信息取出来存下来
                    String deviceId = requestData.getTradeData().getDeviceId();
                    String deviceType = requestData.getTradeData().getDeviceType();
                    String isOpen = requestData.getTradeData().getIsOpen();
                    //客户的唯一id
                    String requestOrderId = requestData.getTradeData().getRequestOrderId();
                    if (deviceId == null || deviceId.length() <= 0 || deviceType == null || deviceType.length() <= 0 || isOpen == null || isOpen.length() <= 0 || connSeqNo == null || connSeqNo.length() <= 0 || token == null || token.length() <= 0 || partnerCode == null || partnerCode.length() <= 0 || tradeTime == null || tradeTime.length() <= 0 || requestOrderId == null) {
                        //参数不全,回复报文
                        System.out.println("请求报文不合法:参数不全！");
                        jsonString = utils.dataWeiKong(ConnSeqNo, curtime);
                        return jsonString;

                    } else {
                        List<RequestOrderOrderList> orderList = null;
                        List<ResponseOrderTradeData> tradeData = new ArrayList<ResponseOrderTradeData>();
                        ResponseOrderTradeData tra = null;
                        String packageCode = "";
                        String orderPeriod = "";
                        String orderUnit = "";
                        String channelOrderID = "";
                        String payAmount = "";
                        // 将请求体json转成String
                        String str = JSON.toJSONString(requestData);
                        // 将Str拿去计算签名
                        String sign = utils.calculateSignature(str);
                        System.out.println("计算的签名是：" + sign);

                        //参数是齐全验证签名：
                        if (!requestSign.equalsIgnoreCase(sign)) {
                            // 如果收到的签名跟算出的签名相同才可以回复正确的报文
                            jsonString = utils.publicMessage(connSeqNo, tradeTime, partnerCode, token, tradeType);
                            return jsonString;

                        } else {
                            //参数齐全，而且签名通过，现在先去把上面取到的数据入库
                            orderList = requestData.getTradeData().getOrderList();
                            if (orderList == null) {
                                System.out.println("请求报文不合法:orderList参数不全！");
                                jsonString = utils.dataWeiKong(ConnSeqNo, curtime);
                                return jsonString;
                            } else {

                                for (RequestOrderOrderList list : orderList) {

                                    packageCode = list.getPackageCode();
                                    orderPeriod = list.getOrderPeriod();
                                    orderUnit = list.getOrderUnit();
                                    payAmount = list.getPayAmount();
                                    channelOrderID = list.getChannelOrderID();

                                    if (StringUtils.isBlank(packageCode) || StringUtils.isBlank(orderPeriod) || StringUtils.isBlank(channelOrderID)) {
                                        System.out.println("请求报文不合法:参数不全！");
                                        jsonString = utils.dataWeiKong(ConnSeqNo, curtime);
                                        return jsonString;

                                    } else {
                                        System.out.println("有没有传价格过来：" + payAmount);
//                                        if (StringUtils.isBlank(payAmount)) { //payAmount==null
//                                            String Amount = internationalPayPackageService.getPackageAmountBypackageCode(packageCode, partnerCode);
//                                            System.out.println("=========================价格：" + Amount);
//                                            //根据前面得到的code去数据库查询name
//                                            String packageName = internationalPayPackageService.getPackageNameBypackageCode(packageCode, partnerCode);
//                                            //String packageName = UexUtil.string2HexUTF8(name);
//
//                                            System.out.println("查询到的套餐名字是：" + packageName);
//                                            String packageType = internationalPayPackageService.getPackageTypeByCode(packageCode, partnerCode);
//                                            //1:数据入库
//                                            UnifiedOrderPackages order = new UnifiedOrderPackages();
//                                            order.setDeviceId(deviceId);
//                                            order.setDeviceType(deviceType);
//                                            order.setPackageName(packageName);
//                                            order.setPackageCode(packageCode);
//                                            order.setOrderPeriod(orderPeriod);
//                                            order.setOrderUnit(orderUnit);
//                                            order.setPayAmount(Amount);
//                                            order.setIsOpen(isOpen);
//                                            order.setChannelOrderID(channelOrderID);
//                                            order.setCreateTime(tradeTime);
//                                            //入库订购订单信息
//                                            unifiedOrderPackagesService.putInOrderInfo(order);
//                                            System.out.println("到这里说明数据库入库操作完成：");
//
//                                            //if (!partnerCode.equals("P000200")) {
//
//                                            System.out.println("走2.0的接口客户：" + partnerCode);
//                                            QueryDeviceRealTimeServiceNew queryDeviceRealTime = new QueryDeviceRealTimeServiceNew();
//                                            DeviceRealTimeInformation information = queryDeviceRealTime.queryDeviceRealTime(deviceId, secret, partnerCode);
//                                            if (information != null) {
//                                                if (true == Constant.RET_CODE_0000.equalsIgnoreCase(information.getRetCode())) {
//
//                                                    if (information.getBuyType().equalsIgnoreCase("2")) {  //后付费套餐可以续订
//
//                                                        if (information.getPackageCode().equals(packageCode)) {//现在要订购的套餐跟正在使用的订单的套餐相同走续订
//
//                                                            String order_id = information.getOrderCode();
//                                                            //3.现在拿到数据了，去数据库更新之前的订单记录
//                                                            UserboundPaymentOrder paymentOrder = new UserboundPaymentOrder();
//                                                            paymentOrder.setPartnerCode(partnerCode);
//                                                            paymentOrder.setPackageCode(packageCode);
//                                                            paymentOrder.setPackageName(packageName);
//                                                            paymentOrder.setOrderId(order_id);
//                                                            paymentOrder.setDeviceId(deviceId);
//                                                            paymentOrder.setDeviceType(deviceType);
//                                                            paymentOrder.setAmount(Amount);
//                                                            paymentOrder.setOrderPeriod(orderPeriod);
//                                                            paymentOrder.setOrderUnit(orderUnit);
//                                                            //续订就是1
//                                                            paymentOrder.setIsRenew("1");
//                                                            paymentOrder.setChannelOrderID(channelOrderID);
//                                                            paymentOrder.setRequestOrderId(requestOrderId);
//                                                            // 根据收到的deviceId去更新数据表，deviceId此时是有的，所以不能为null,openid为空
//                                                            userboundPaymentOrderService.UpdateRenewPackagesOrder(paymentOrder);
//                                                            System.out.println("到这里说明数据库更新操作完成：");
//
//                                                            tra = new ResponseOrderTradeData();
//                                                            tra.setChannelOrderID(channelOrderID);
//                                                            tra.setOrderId(information.getOrderCode());
//                                                            tradeData.add(tra);
//
//                                                            ResponseOrderData data = new ResponseOrderData();
//                                                            data.setConnSeqNo(ConnSeqNo);
//                                                            data.setPartnerCode(partnerCode);
//                                                            data.setToken(token);
//                                                            data.setTradeType(tradeType);
//                                                            data.setTradeTime(tradeTime);
//                                                            data.setTradeRstCode(1000);
//                                                            data.setTradeRstMessage("成功");
//                                                            data.setTradeData(tradeData);
//
//                                                            ResponseOrder responseOrder = new ResponseOrder();
//                                                            responseOrder.setData(data);
//                                                            responseOrder.setSign(MD5.calcMD5(JSON.toJSONString(data)));
//
//                                                            // 返回给调用者的报文
//                                                            jsonString = JSON.toJSONString(responseOrder);
//                                                            System.out.println("响应报文：" + jsonString);
//                                                            return jsonString;
//
//
//                                                        } else {//现在要订购的套餐跟正在使用的订单的套餐不相同走新订
//                                                            OrderServiceNew turnToOrderService = new OrderServiceNew();
//                                                            OrderPaymentManager user = turnToOrderService.orderPackage(partnerCode, deviceId, packageName, packageCode, packageType, orderPeriod, Amount, secret);
//
//                                                            if (user != null) {
//                                                                if (true == Constant.RET_CODE_0000.equalsIgnoreCase(user.getRetCode())) {
//
//                                                                    //3.现在拿到数据了，去数据库更新之前的订单记录
//                                                                    order.setOrderId(user.getOrderId());
//                                                                    order.setOrderStartDate(user.getOrderStartDate());
//                                                                    order.setOrderExpireDate(user.getOrderExpireDate());
//                                                                    order.setUpdateTime(tradeTime);
//                                                                    unifiedOrderPackagesService.updateOrderInfo(order);
//
//                                                                    //下单存库
//                                                                    UserboundPaymentOrder paymentOrder = new UserboundPaymentOrder();
//                                                                    paymentOrder.setPartnerCode(partnerCode);
//                                                                    paymentOrder.setPackageCode(packageCode);
//                                                                    paymentOrder.setPackageName(packageName);
//                                                                    paymentOrder.setOrderId(user.getOrderId());
//                                                                    paymentOrder.setDeviceId(deviceId);
//                                                                    paymentOrder.setDeviceType(deviceType);
//                                                                    paymentOrder.setAmount(Amount);
//                                                                    paymentOrder.setOrderPeriod(orderPeriod);
//                                                                    paymentOrder.setOrderUnit(orderUnit);
//                                                                    paymentOrder.setChannelOrderID(channelOrderID);
//                                                                    paymentOrder.setRequestOrderId(requestOrderId);
//                                                                    // 根据收到的deviceId去更新数据表，deviceId此时是有的，所以不能为null,openid为空
//                                                                    userboundPaymentOrderService.InsertPaymentOrder(paymentOrder);
//
//                                                                    tra = new ResponseOrderTradeData();
//                                                                    tra.setChannelOrderID(channelOrderID);
//                                                                    tra.setOrderId(user.getOrderId());
//                                                                    tra.setOrderStartDate(user.getOrderStartDate());
//                                                                    tra.setOrderExpireDate(user.getOrderExpireDate());
//                                                                    tradeData.add(tra);
//
//                                                                    ResponseOrderData data = new ResponseOrderData();
//                                                                    data.setConnSeqNo(ConnSeqNo);
//                                                                    data.setPartnerCode(partnerCode);
//                                                                    data.setToken(token);
//                                                                    data.setTradeType(tradeType);
//                                                                    data.setTradeTime(tradeTime);
//                                                                    data.setTradeRstCode(1000);
//                                                                    data.setTradeRstMessage("成功");
//                                                                    data.setTradeData(tradeData);
//
//                                                                    ResponseOrder responseOrder = new ResponseOrder();
//                                                                    responseOrder.setData(data);
//                                                                    responseOrder.setSign(MD5.calcMD5(JSON.toJSONString(data)));
//
//                                                                    // 返回给调用者的报文
//                                                                    jsonString = JSON.toJSONString(responseOrder);
//                                                                    System.out.println("响应报文：" + jsonString);
//                                                                    return jsonString;
//
//
//                                                                } else {
//
//                                                                    System.out.println("下单返回数据有问题");
//                                                                    jsonString = utils.exceptionReturn(connSeqNo, curtime, tradeType, partnerCode, token, user.getRetCode(), user.getResultInfo());
//                                                                    return jsonString;
//
//                                                                }
//
//                                                            } else {
//                                                                System.out.println("下单有问题");
//                                                                jsonString = utils.unknownError(connSeqNo, curtime, tradeType);
//                                                                return jsonString;
//
//                                                            }
//
//                                                        }
//
//
//                                                    } else { //预付费套餐还有日程套餐不可续订就直接走正式下单
//
//                                                        OrderServiceNew turnToOrderService = new OrderServiceNew();
//                                                        OrderPaymentManager user = turnToOrderService.orderPackage(partnerCode, deviceId, packageName, packageCode, packageType, orderPeriod, Amount, secret);
//                                                        if (user != null) {
//                                                            if (true == Constant.RET_CODE_0000.equalsIgnoreCase(user.getRetCode())) {
//
//                                                                //3.现在拿到数据了，去数据库更新之前的订单记录
//                                                                order.setOrderId(user.getOrderId());
//                                                                order.setOrderStartDate(user.getOrderStartDate());
//                                                                order.setOrderExpireDate(user.getOrderExpireDate());
//                                                                order.setUpdateTime(tradeTime);
//                                                                unifiedOrderPackagesService.updateOrderInfo(order);
//                                                                //下单存库
//                                                                UserboundPaymentOrder paymentOrder = new UserboundPaymentOrder();
//                                                                paymentOrder.setPartnerCode(partnerCode);
//                                                                paymentOrder.setPackageCode(packageCode);
//                                                                paymentOrder.setPackageName(packageName);
//                                                                paymentOrder.setOrderId(user.getOrderId());
//                                                                paymentOrder.setDeviceId(deviceId);
//                                                                paymentOrder.setDeviceType(deviceType);
//                                                                paymentOrder.setAmount(Amount);
//                                                                paymentOrder.setOrderPeriod(orderPeriod);
//                                                                paymentOrder.setOrderUnit(orderUnit);
//                                                                paymentOrder.setChannelOrderID(channelOrderID);
//                                                                paymentOrder.setRequestOrderId(requestOrderId);
//                                                                // 根据收到的deviceId去更新数据表，deviceId此时是有的，所以不能为null,openid为空
//                                                                userboundPaymentOrderService.InsertPaymentOrder(paymentOrder);
//
//                                                                tra = new ResponseOrderTradeData();
//                                                                tra.setChannelOrderID(channelOrderID);
//                                                                tra.setOrderId(user.getOrderId());
//                                                                tra.setOrderStartDate(user.getOrderStartDate());
//                                                                tra.setOrderExpireDate(user.getOrderExpireDate());
//                                                                tradeData.add(tra);
//
//                                                                ResponseOrderData data = new ResponseOrderData();
//                                                                data.setConnSeqNo(ConnSeqNo);
//                                                                data.setPartnerCode(partnerCode);
//                                                                data.setToken(token);
//                                                                data.setTradeType(tradeType);
//                                                                data.setTradeTime(tradeTime);
//                                                                data.setTradeRstCode(1000);
//                                                                data.setTradeRstMessage("成功");
//                                                                data.setTradeData(tradeData);
//
//                                                                ResponseOrder responseOrder = new ResponseOrder();
//                                                                responseOrder.setData(data);
//                                                                responseOrder.setSign(MD5.calcMD5(JSON.toJSONString(data)));
//
//                                                                // 返回给调用者的报文
//                                                                jsonString = JSON.toJSONString(responseOrder);
//                                                                System.out.println("响应报文：" + jsonString);
//                                                                return jsonString;
//
//
//                                                            } else {
//
//                                                                System.out.println("下单返回数据有问题");
//                                                                jsonString = utils.exceptionReturn(connSeqNo, curtime, tradeType, partnerCode, token, user.getRetCode(), user.getResultInfo());
//                                                                return jsonString;
//
//                                                            }
//
//                                                        } else {
//                                                            System.out.println("下单有问题");
//                                                            jsonString = utils.unknownError(connSeqNo, curtime, tradeType);
//                                                            return jsonString;
//
//                                                        }
//
//
//                                                    }
//
//
//                                                } else {
//                                                    System.out.println("返回的数据有问题");
//                                                    jsonString = utils.exceptionReturn(connSeqNo, curtime, tradeType, partnerCode, token, information.getRetCode(), information.getResultInfo());
//                                                    return jsonString;
//
//
//                                                }
//
//                                            } else {
//                                                System.out.println("查询就有问题");
//                                                jsonString = utils.unknownError(connSeqNo, curtime, tradeType);
//                                                return jsonString;
//
//                                            }
//
//                                        } else { // payAmount!=null

                                        //根据前面得到的code去数据库查询name
                                        String packageName = internationalPayPackageService.getPackageNameBypackageCode(packageCode, partnerCode);
                                        //String packageName = UexUtil.string2HexUTF8(name);

                                        System.out.println("查询到的套餐名字是：" + packageName);
                                        String packageType = internationalPayPackageService.getPackageTypeByCode(packageCode, partnerCode);
                                        //1:数据入库
                                        UnifiedOrderPackages order = new UnifiedOrderPackages();
                                        order.setDeviceId(deviceId);
                                        order.setDeviceType(deviceType);
                                        order.setPackageName(packageName);
                                        order.setPackageCode(packageCode);
                                        order.setOrderPeriod(orderPeriod);
                                        order.setOrderUnit(orderUnit);
                                        order.setPayAmount(payAmount);
                                        order.setIsOpen(isOpen);
                                        order.setChannelOrderID(channelOrderID);
                                        order.setCreateTime(tradeTime);
                                        //入库订购订单信息
                                        unifiedOrderPackagesService.putInOrderInfo(order);
                                        System.out.println("到这里说明数据库入库操作完成：");

                                        //入库
                                        UserboundPaymentOrder userboundPaymentOrder=new UserboundPaymentOrder();
                                        userboundPaymentOrder.setPartnerCode(partnerCode);
                                        userboundPaymentOrder.setDeviceId(deviceId);
                                        userboundPaymentOrder.setDeviceType(deviceType);
                                        userboundPaymentOrder.setPackageName(packageName);
                                        userboundPaymentOrder.setPackageCode(packageCode);
                                        userboundPaymentOrder.setOrderPeriod(orderPeriod);
                                        userboundPaymentOrder.setOrderUnit(orderUnit);
                                        userboundPaymentOrder.setRequestOrderId(requestOrderId);
                                        userboundPaymentOrder.setChannelOrderID(channelOrderID);
                                        userboundPaymentOrder.setIsOpen(isOpen);
                                        userboundPaymentOrder.setAmount(payAmount);
                                        userboundPaymentOrderService.InsertUserOrder(userboundPaymentOrder);

                                        //if (!partnerCode.equals("P000200")) {

                                        System.out.println("走2.0的接口客户：" + partnerCode);
                                        QueryDeviceRealTimeServiceNew queryDeviceRealTime = new QueryDeviceRealTimeServiceNew();
                                        DeviceRealTimeInformation information = queryDeviceRealTime.queryDeviceRealTime(deviceId, secret, partnerCode);

                                        if (information != null) {
                                            if (true == Constant.RET_CODE_0000.equalsIgnoreCase(information.getRetCode())) {

                                                if (information.getBuyType().equalsIgnoreCase("2")) {  //后付费套餐可以续订
                                                    if (information.getPackageCode().equals(packageCode)) {//现在要订购的套餐跟正在使用的订单的套餐相同走续订

                                                        String order_id = information.getOrderCode();
                                                        //3.现在拿到数据了，去数据库更新之前的订单记录
                                                        UserboundPaymentOrder paymentOrder = new UserboundPaymentOrder();
                                                        paymentOrder.setPartnerCode(partnerCode);
                                                        paymentOrder.setPackageCode(packageCode);
                                                        paymentOrder.setOrderId(order_id);
                                                        paymentOrder.setDeviceId(deviceId);
                                                        //续订就是1
                                                        paymentOrder.setIsRenew("1");
                                                        paymentOrder.setChannelOrderID(channelOrderID);
                                                        paymentOrder.setRequestOrderId(requestOrderId);
                                                        // 根据收到的deviceId去更新数据表，deviceId此时是有的，所以不能为null,openid为空
                                                        userboundPaymentOrderService.UpdateRenewPackagesOrder(paymentOrder);

                                                        System.out.println("到这里说明数据库更新操作完成：");

                                                        tra = new ResponseOrderTradeData();
                                                        tra.setChannelOrderID(channelOrderID);
                                                        tra.setOrderId(order_id);
                                                        tradeData.add(tra);

                                                        ResponseOrderData data = new ResponseOrderData();
                                                        data.setConnSeqNo(ConnSeqNo);
                                                        data.setPartnerCode(partnerCode);
                                                        data.setToken(token);
                                                        data.setTradeType(tradeType);
                                                        data.setTradeTime(tradeTime);
                                                        data.setTradeRstCode(1000);
                                                        data.setTradeRstMessage("成功");
                                                        data.setTradeData(tradeData);

                                                        ResponseOrder responseOrder = new ResponseOrder();
                                                        responseOrder.setData(data);
                                                        responseOrder.setSign(MD5.calcMD5(JSON.toJSONString(data)));

                                                        // 返回给调用者的报文
                                                        jsonString = JSON.toJSONString(responseOrder);
                                                        System.out.println("响应报文：" + jsonString);
                                                        return jsonString;


                                                    } else {//现在要订购的套餐跟正在使用的订单的套餐不相同走新订
                                                        OrderServiceNew turnToOrderService = new OrderServiceNew();
                                                        OrderPaymentManager user = turnToOrderService.orderPackage(partnerCode, deviceId, packageName, packageCode, packageType, orderPeriod, payAmount, secret);

                                                        if (user != null) {
                                                            if (true == Constant.RET_CODE_0000.equalsIgnoreCase(user.getRetCode())) {

                                                                //3.现在拿到数据了，去数据库更新之前的订单记录
                                                                order.setOrderId(user.getOrderId());
                                                                order.setOrderStartDate(user.getOrderStartDate());
                                                                order.setOrderExpireDate(user.getOrderExpireDate());
                                                                order.setUpdateTime(tradeTime);
                                                                unifiedOrderPackagesService.updateOrderInfo(order);

                                                                //下单存库
//                                                                UserboundPaymentOrder paymentOrder = new UserboundPaymentOrder();
//                                                                paymentOrder.setPartnerCode(partnerCode);
//                                                                paymentOrder.setPackageCode(packageCode);
//                                                                paymentOrder.setPackageName(packageName);
//                                                                paymentOrder.setOrderId(user.getOrderId());
//                                                                paymentOrder.setDeviceId(deviceId);
//                                                                paymentOrder.setDeviceType(deviceType);
//                                                                paymentOrder.setAmount(payAmount);
//                                                                paymentOrder.setOrderPeriod(orderPeriod);
//                                                                paymentOrder.setOrderUnit(orderUnit);
//                                                                paymentOrder.setChannelOrderID(channelOrderID);
//                                                                paymentOrder.setRequestOrderId(requestOrderId);
//                                                                // 根据收到的deviceId去更新数据表，deviceId此时是有的，所以不能为null,openid为空
//                                                                userboundPaymentOrderService.InsertPaymentOrder(paymentOrder);
                                                                //3.现在拿到数据了，去数据库更新之前的订单记录
                                                                UserboundPaymentOrder paymentOrder = new UserboundPaymentOrder();
                                                                paymentOrder.setPartnerCode(partnerCode);
                                                                paymentOrder.setPackageCode(packageCode);
                                                                paymentOrder.setOrderId(user.getOrderId());
                                                                paymentOrder.setDeviceId(deviceId);
                                                                //新订是0
                                                                paymentOrder.setIsRenew("0");
                                                                paymentOrder.setChannelOrderID(channelOrderID);
                                                                paymentOrder.setRequestOrderId(requestOrderId);
                                                                // 根据收到的deviceId去更新数据表，deviceId此时是有的，所以不能为null,openid为空
                                                                userboundPaymentOrderService.UpdateRenewPackagesOrder(paymentOrder);


                                                                tra = new ResponseOrderTradeData();
                                                                tra.setChannelOrderID(channelOrderID);
                                                                tra.setOrderId(user.getOrderId());
                                                                tra.setOrderStartDate(user.getOrderStartDate());
                                                                tra.setOrderExpireDate(user.getOrderExpireDate());
                                                                tradeData.add(tra);

                                                                ResponseOrderData data = new ResponseOrderData();
                                                                data.setConnSeqNo(ConnSeqNo);
                                                                data.setPartnerCode(partnerCode);
                                                                data.setToken(token);
                                                                data.setTradeType(tradeType);
                                                                data.setTradeTime(tradeTime);
                                                                data.setTradeRstCode(1000);
                                                                data.setTradeRstMessage("成功");
                                                                data.setTradeData(tradeData);

                                                                ResponseOrder responseOrder = new ResponseOrder();
                                                                responseOrder.setData(data);
                                                                responseOrder.setSign(MD5.calcMD5(JSON.toJSONString(data)));

                                                                // 返回给调用者的报文
                                                                jsonString = JSON.toJSONString(responseOrder);
                                                                System.out.println("响应报文：" + jsonString);
                                                                return jsonString;


                                                            } else {

                                                                System.out.println("下单返回数据有问题");
                                                                jsonString = utils.exceptionReturn(connSeqNo, curtime, tradeType, partnerCode, token, user.getRetCode(), user.getResultInfo());
                                                                return jsonString;

                                                            }

                                                        } else {
                                                            System.out.println("下单有问题");
                                                            jsonString = utils.unknownError(connSeqNo, curtime, tradeType);
                                                            return jsonString;

                                                        }

                                                    }


                                                } else { //预付费套餐还有日程套餐不可续订就直接走正式下单

                                                    OrderServiceNew turnToOrderService = new OrderServiceNew();
                                                    OrderPaymentManager user = turnToOrderService.orderPackage(partnerCode, deviceId, packageName, packageCode, packageType, orderPeriod, payAmount, secret);

                                                    if (user != null) {
                                                        if (true == Constant.RET_CODE_0000.equalsIgnoreCase(user.getRetCode())) {

                                                            //3.现在拿到数据了，去数据库更新之前的订单记录
                                                            order.setOrderId(user.getOrderId());
                                                            order.setOrderStartDate(user.getOrderStartDate());
                                                            order.setOrderExpireDate(user.getOrderExpireDate());
                                                            order.setUpdateTime(tradeTime);
                                                            unifiedOrderPackagesService.updateOrderInfo(order);

                                                            //下单存库
//                                                            UserboundPaymentOrder paymentOrder = new UserboundPaymentOrder();
//                                                            paymentOrder.setPartnerCode(partnerCode);
//                                                            paymentOrder.setPackageCode(packageCode);
//                                                            paymentOrder.setPackageName(packageName);
//                                                            paymentOrder.setOrderId(user.getOrderId());
//                                                            paymentOrder.setDeviceId(deviceId);
//                                                            paymentOrder.setDeviceType(deviceType);
//                                                            paymentOrder.setAmount(payAmount);
//                                                            paymentOrder.setOrderPeriod(orderPeriod);
//                                                            paymentOrder.setOrderUnit(orderUnit);
//                                                            paymentOrder.setChannelOrderID(channelOrderID);
//                                                            paymentOrder.setRequestOrderId(requestOrderId);
//                                                            // 根据收到的deviceId去更新数据表，deviceId此时是有的，所以不能为null,openid为空
//                                                            userboundPaymentOrderService.InsertPaymentOrder(paymentOrder);

                                                            UserboundPaymentOrder paymentOrder = new UserboundPaymentOrder();
                                                            paymentOrder.setPartnerCode(partnerCode);
                                                            paymentOrder.setPackageCode(packageCode);
                                                            paymentOrder.setOrderId(user.getOrderId());
                                                            paymentOrder.setDeviceId(deviceId);
                                                            //新订是0
                                                            paymentOrder.setIsRenew("0");
                                                            paymentOrder.setChannelOrderID(channelOrderID);
                                                            paymentOrder.setRequestOrderId(requestOrderId);
                                                            // 根据收到的deviceId去更新数据表，deviceId此时是有的，所以不能为null,openid为空
                                                            userboundPaymentOrderService.UpdateRenewPackagesOrder(paymentOrder);


                                                            tra = new ResponseOrderTradeData();
                                                            tra.setChannelOrderID(channelOrderID);
                                                            tra.setOrderId(user.getOrderId());
                                                            tra.setOrderStartDate(user.getOrderStartDate());
                                                            tra.setOrderExpireDate(user.getOrderExpireDate());
                                                            tradeData.add(tra);

                                                            ResponseOrderData data = new ResponseOrderData();
                                                            data.setConnSeqNo(ConnSeqNo);
                                                            data.setPartnerCode(partnerCode);
                                                            data.setToken(token);
                                                            data.setTradeType(tradeType);
                                                            data.setTradeTime(tradeTime);
                                                            data.setTradeRstCode(1000);
                                                            data.setTradeRstMessage("成功");
                                                            data.setTradeData(tradeData);

                                                            ResponseOrder responseOrder = new ResponseOrder();
                                                            responseOrder.setData(data);
                                                            responseOrder.setSign(MD5.calcMD5(JSON.toJSONString(data)));

                                                            // 返回给调用者的报文
                                                            jsonString = JSON.toJSONString(responseOrder);
                                                            System.out.println("响应报文：" + jsonString);
                                                            return jsonString;


                                                        } else {

                                                            System.out.println("下单返回数据有问题");
                                                            jsonString = utils.exceptionReturn(connSeqNo, curtime, tradeType, partnerCode, token, user.getRetCode(), user.getResultInfo());
                                                            return jsonString;

                                                        }

                                                    } else {
                                                        System.out.println("下单有问题");
                                                        jsonString = utils.unknownError(connSeqNo, curtime, tradeType);
                                                        return jsonString;

                                                    }


                                                }


                                            } else {
                                                System.out.println("返回的数据有问题");
                                                jsonString = utils.exceptionReturn(connSeqNo, curtime, tradeType, partnerCode, token, information.getRetCode(), information.getResultInfo());
                                                return jsonString;


                                            }

                                        } else {
                                            System.out.println("查询就有问题");
                                            jsonString = utils.unknownError(connSeqNo, curtime, tradeType);
                                            return jsonString;

                                        }


                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return null;
    }
}